package zjut.raymond.witdemo.ui.adapter;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import zjut.raymond.witdemo.R;
import zjut.raymond.witdemo.pojo.ReferencePoint;

public class RPRecyclerViewAdapter extends RecyclerView.Adapter<RPRecyclerViewAdapter.MyViewHolder>{
    List<ReferencePoint> referencePoints = new ArrayList<>();

    public void setReferencePoints(List<ReferencePoint> referencePoints) {
        this.referencePoints = referencePoints;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
//        View itemView = layoutInflater.inflate(R.layout.my_recyclerview_item, parent, false);
        View itemView = layoutInflater.inflate(R.layout.item_rplist_recyclerview, parent, false);
        return new MyViewHolder(itemView);
        //return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        final ReferencePoint referencePoint = referencePoints.get(position);
        holder.tvOrder.setText(String.valueOf(position + 1));
        String idStr = "id: " + referencePoint.getId();
        holder.tvId.setText(idStr);
        String coordinate = "坐标: (" + referencePoint.getX() + "," + referencePoint.getY() + ")";
        holder.tvCoord.setText(coordinate);
        referencePoint.getUpdateTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        holder.tvUpdateTime.setText("更新时间：" + simpleDateFormat.format(referencePoint.getUpdateTime()));
        holder.itemView.setOnClickListener(v -> {
            Log.d("TAG", "onClick: " + referencePoint.getId() + " " + coordinate);
            Bundle b = new Bundle();
            b.putInt("rp_id", referencePoint.getId());
            b.putString("rp_coord", coordinate);
            NavController controller = Navigation.findNavController(v);
            controller.navigate(R.id.action_listFragment_to_collectorFragment, b);
        });
    }

    @Override
    public int getItemCount() {
        return referencePoints.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvOrder, tvId, tvCoord, tvUpdateTime;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvOrder = itemView.findViewById(R.id.tv_order);
            tvId = itemView.findViewById(R.id.tv_rp_id);
            tvCoord = itemView.findViewById(R.id.tv_rp_coord);
            tvUpdateTime = itemView.findViewById(R.id.tv_update_time);
        }
    }
}
